﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     Menu业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class MenuBLL
    {
        private static MenuBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IMenuDao dao = null;

        private MenuBLL()
        {
            dao = MenuSqlMapDao.Instance;
        }

        public static MenuBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new MenuBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<Menu> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<Menu> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<Menu> GetListByParameters(object TopCounts, Menu menu)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<Menu> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        public System.Collections.Generic.IList<Menu> GetListByParameters(object TopCounts, string id ,string aaa)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<Menu> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public Menu GetDetail(System.String menuid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(menuid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public object Add(Menu menu)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;

            try
            {
                ///daoManager.BeginTransaction();

                return dao.Add(menu);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public int Update(Menu menu)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(menu);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public int Delete(System.String menuid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(menuid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public int Delete(Menu menu)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(menu);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 根据父ID查询
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public IList<Menu> GetMenuByParentID(string parentID)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("parentID", parentID);
                ht.Add("state", Common.Enum.auvState.Enabled);
                ht.Add("isDel", Common.Enum.auvDel.NotDel);
                return dao.GetListByParameters(ht);
            }
            catch 
            {
                
                throw;
            }
        }

        /// <summary> 
        /// 根据菜单名称获取对象
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public Menu GetMenuByName(string menuName)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("menuName", menuName);
                IList<Menu> list = dao.GetListByParameters(ht);
                if (list.Count > 0)
                {
                    return list[0];
                }
                else
                {
                    return null;
                }
            }
            catch 
            {
                
                throw;
            }
        }

        public string GetParentMenu(string menuid)
        {
            try
            {
                Menu menu = dao.GetDetail(menuid);
                if (menu != null)
                {
                    if (menu.ParentID != "0")
                    {
                        return dao.GetDetail(menu.ParentID).MenuName;
                    }
                }
                return "无";
            }
            catch 
            {
                
                throw;
            }
        }
    }
}
